#!/usr/bin/env python
# coding=utf-8
# @Time    : 2024/6/13 15:27
# @Author  : 江斌
# @Software: PyCharm
import logging
import os
import json
import time
import openai  # 1.30.5
logger = logging.getLogger("common.logger")
openai.api_key = os.getenv("API_KEY", 'sk-jcxOaTOm0SqppaCLAdF5Bb1a4c864a4aA8C58e71D9E76476')
openai.base_url = os.getenv("API_BASE_URL", "https://llmapis.xmov.ai/v1/")
openai.default_headers = {"Accept": "application/json, text/plain, */*"}


def ask(question="what's the weather?", model="gpt-4o"):
    """
    :param question:
    :param model: gpt-4o, skylark2-lite-8k, skylark2-pro-32k
    :return:
    """
    completion = openai.chat.completions.create(
        model=model,
        messages=[{
            "role": "user",
            "content": question}
        ]
    )
    return completion.choices[0].message.content


def get_gpt_examples(keyword, num=3):
    question = '给我%s个关于%s的英文例句，只列出英文和中文翻译。返回格式为json，{"results":[{"en":"","cn":""}]},不需要```json ```' % (num, keyword)
    data = ask(question)
    return json.loads(data)


if __name__ == '__main__':
    st = time.time()
    print(get_gpt_examples("new normal"))
    print(f"cost: {time.time() - st}s")
